// pages/coupon/coupon.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectIndex: 1,
    myCoupon: {},
    allCoupon: {},
    info:{},
    isBindtap: true,
    prePage: '',
    navigateData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let pages = getCurrentPages(),
        prePage = pages[pages.length - 2].route
    this.setData({
      prePage: prePage
    })

    if(options.tab) {
      this.setData({
        selectIndex: options.tab
      })
    }

    if (options.info) {
      this.setData({
        info: JSON.parse(options.info)
      })
    }

    if (this.isEmptyObject(this.data.info)) {
      this.setData({
        isBindtap: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initApp()
  },

  /**
   * 
   */
  tabsClick: function(e) {
    let that = this
    if(that.data.selectIndex == e.target.dataset.id) {
      return false
    } else {
      that.setData({
        selectIndex: e.target.dataset.id
      })
      if (that.data.selectIndex == 1) {
        that.getCoupon('self')
      } else {
        that.getCoupon('all')
      }
    }
  },

  /**
   * selectTickets
   */
  selectTickets: function(e) {
    let that = this,
        cp = e.currentTarget.dataset.cp

    if (that.data.info.totalPrice > cp.use_price) {
      this.setData({
        'info.coupon': cp
      })
      wx.navigateTo({
        url: '/' + that.data.prePage + '?data=' + JSON.stringify(that.data.info),
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '不满足使用规则',
      })

      return false
    }
    
    
  },

  /**
   * init
   */
  initApp:function() {
    let that = this
    that.getCoupon("self")
  },

  /**
   * getCoupon
   */
  getCoupon: function(type) {
    let that = this,
        date = new Date().getTime(),
        coupon = [],
        data,k
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      if (type == 'self') {
        wx.request({
          url: app.globalData.hostAPI + '/v1/customer/coupon?access_token=' + accessToken,
          data: {
            type: type
          },
          success: res => {
            if(res.data.status === 1) {
              for (k in res.data.data.coupon) {
                if (new Date(res.data.data.coupon[k].expired_at).getTime() > date) {
                  coupon.push(res.data.data.coupon[k])
                }
              }
              that.setData({
                myCoupon: coupon
              })
              wx.hideLoading()
            }
          }
        })
      }

      if (type == 'all') {
        wx.request({
          url: app.globalData.hostAPI + '/v1/coupon/list?access_token=' + accessToken,
          data: {
            type: type
          },
          success: res => {
            if(res.data.status === 1) {
              that.setData({
                allCoupon: res.data.data
              })
            }
            wx.hideLoading()
          }
        })
      }
    })
  },

  /**
   * 
   */
  takeCoupon: function(e) {
    let that = this,
        id = e.currentTarget.id

    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })

    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/coupon/exchange-point?access_token=' + accessToken,
        data: {
          coupon_id: id,
        },
        success: res => {
          if(res.data.status === 1) {
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: '兑换成功',
              success: res => {
                if(res.confirm) {
                  that.setData({
                    selectIndex: 1
                  })
                  that.getCoupon('self')
                }
              }
            })
          }
        }
      })
    })
  },

  /**
   * 
   */
  isEmptyObject: function(obj) {
    let name
    for (name in obj) {
      return false
    }
    return true
  }
})